/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.dinner.web.shop;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.entity.Result;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.dinner.entity.shop.ResShop;
import com.thinkgem.jeesite.modules.dinner.service.shop.ResShopService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 店铺信息Controller
 * @author dai
 * @version 2018-11-26
 */
@Controller
@RequestMapping(value = "${adminPath}/mobile/dinner/shop/resShop")
public class ResShopMobileController extends BaseController {

	@Autowired
	private ResShopService resShopService;
	
	@ModelAttribute
	public ResShop get(@RequestParam(required=false) String id) {
		ResShop entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = resShopService.get(id);
		}
		if (entity == null){
			entity = new ResShop();
		}
		return entity;
	}
	
	//@RequiresPermissions("dinner:shop:resShop:view")
	@RequestMapping(value = {"list", ""})
	@ResponseBody
	public Result list(HttpServletRequest request, HttpServletResponse response) {
		ResShop resShop = new ResShop();
		Page<ResShop> page = resShopService.findPage(new Page<ResShop>(request, response), resShop);
		return new Result(page.getList());
	}

	@RequiresPermissions("dinner:shop:resShop:view")
	@RequestMapping(value = "form")
	public String form(ResShop resShop, Model model) {
		model.addAttribute("resShop", resShop);
		return "modules/dinner/shop/resShopForm";
	}

	@RequiresPermissions("dinner:shop:resShop:edit")
	@RequestMapping(value = "save")
	public String save(ResShop resShop, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, resShop)){
			return form(resShop, model);
		}
		resShopService.save(resShop);
		addMessage(redirectAttributes, "保存店铺成功");
		return "redirect:"+Global.getAdminPath()+"/dinner/shop/resShop/?repage";
	}
	
	@RequiresPermissions("dinner:shop:resShop:edit")
	@RequestMapping(value = "delete")
	public String delete(ResShop resShop, RedirectAttributes redirectAttributes) {
		resShopService.delete(resShop);
		addMessage(redirectAttributes, "删除店铺成功");
		return "redirect:"+Global.getAdminPath()+"/dinner/shop/resShop/?repage";
	}

}